package com.wedu.modules.test.controller;

import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.R;
import com.wedu.common.validator.ValidatorUtils;
import com.wedu.modules.sys.controller.AbstractController;
import com.wedu.modules.sys.entity.SysUserEntity;
import com.wedu.modules.sys.service.SysUserService;
import com.wedu.modules.test.entity.StudentScoreEntity;
import com.wedu.modules.test.entity.StudentVO;
import com.wedu.modules.test.service.StudentScoreService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("test/studentscore")
public class StudentScoreController extends AbstractController {

    @Autowired
    private StudentScoreService studentScoreService;

    @Autowired
    private SysUserService sysUserService;
    /**
     * 查看所有信息
     * */
    @GetMapping("/list")
    @RequiresPermissions("test:studentscore:list")
    public R list(@RequestParam Map<String,Object> params){
        Long userId = getUserId();
        PageUtils pageUtils = studentScoreService.queryPagestu(params,userId);
        Integer student = getUser().getStudent();

        List<StudentScoreEntity> studentScoreEntityList = studentScoreService.list();
        List<Map<String,Object>> timeList = new ArrayList<>();
        for (StudentScoreEntity studentScoreEntity : studentScoreEntityList) {
            Map<String,Object> map = new HashMap<>();
            map.put("examTime",studentScoreEntity.getExamTime());
            if (!timeList.contains(map)){
                timeList.add(map);
            }
//            timeList.add(map);
        }
        R r = R.ok();
        r.put("page",pageUtils);
        r.put("student",student);
        r.put("examTime",timeList);

        return r;
    }

    /**
     * 根据id查看信息
     * */
    @GetMapping("/info/{id}")
    @RequiresPermissions("test:studentscore:info")
    public R info(@PathVariable("id") Long id){
        StudentScoreEntity studentScoreEntity = studentScoreService.getById(id);
        return R.ok().put("studentscore",studentScoreEntity);
    }

    //保存信息
    @PostMapping("/save")
    @RequiresPermissions("test:studentscore:save")
    public R save(@RequestBody StudentScoreEntity studentScoreEntity){
        studentScoreEntity.setCreateTime(new Date());
        studentScoreEntity.setCreateBy(getUserId());
        R r = studentScoreService.savestu(studentScoreEntity);
        return r;
    }

    //修改
    @PostMapping("/update")
    @RequiresPermissions("test:studentscore:update")
    public R update(@RequestBody StudentScoreEntity studentScoreEntity){
        studentScoreEntity.setUpdateTime(new Date());
        studentScoreEntity.setUpdateBy(getUserId());
        studentScoreService.updatestu(studentScoreEntity);
        return R.ok();

    }

    //删除
    @PostMapping("/deleted")
    @RequiresPermissions("test:studentscore:deleted")
    public R deleted(@RequestBody Long[] ids){
        studentScoreService.deletedstu(ids);
        return R.ok();
    }

    /**
     * 将id与真实姓名存入map
     * */
    @GetMapping("/userlist")
    @RequiresPermissions("test:studentscore:list")
    public R userlist(){
        List<SysUserEntity> sysUserEntityList = studentScoreService.getuserRealName();
        List<Map<String,Object>> userlist = new ArrayList<>();
        for (SysUserEntity sysUserEntity : sysUserEntityList) {
            Map<String,Object> map = new HashMap<>();
            map.put("userId",sysUserEntity.getUserId());
            map.put("realName",sysUserEntity.getRealName());
            userlist.add(map);
        }

        R r = R.ok();
        r.put("userlist",userlist);
        return r;
    }
//    public R userlist(@RequestBody Map<String,Object> usermap){
//        List<Map<String,Object>> userlist = new ArrayList<>();
//        List<SysUserEntity> userRealname = studentScoreService.getuserRealName();
//        for (SysUserEntity sysUserEntity : userRealname) {
//            Map<String,Object> userName = new HashMap<>();
//            userName.put("userId",sysUserEntity.getUserId());
//            userName.put("realName",sysUserEntity.getRealName());
//            userlist.add(userName);
//        }
//        return R.ok().put("userlist",userlist);
//    }
}
